<template>
  <div
    class='com-chart'
    ref='article_learn_ref'
    :style="{width: '100%' , height:'100%'}"
  ></div>
</template>

<script>
import homeview from "@/api/homeview";
export default {
  data() {
    return {
      chartInstance: null,
      allData: {},
      startValue: 0, // 区域缩放的起点值
      endValue: 5, // 区域缩放的终点值
      timerId: null, // 定时器的标识
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timerId);
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.article_learn_ref);
      const initOption = {
        grid: {
          top: "30",
          left: "5%",
          right: "12%",
          bottom: "4%",
          containLabel: true,
        },
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
          name: this.$t('like.xname'),
          nameLocation: "end",
          axisTick: {
            show: false, // 去除轴下面的刻度
          },
          axisLabel: {
            interval: 0,
            inside: false,
            formatter: function (params) {
              var val = "";
              if (params.length > 4) {
                //截取4个字
                val = params.substr(0, 4) + "...";
                return val;
              } else {
                return params;
              }
            },
          },
        },
        yAxis: {
          type: "value",
          name: this.$t('like.title'),
          nameLocation: "end",
          minInterval: 1,
          axisLine: {
            show: true,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            lineStyle: {
              type: "dotted",
              color: "#eee",
            },
          },
        },
        series: [
          {
            type: "bar",
            name:this.$t('like.arl'),
            itemStyle: {
              color: "#1F0F50",
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    getData() {
      homeview
        .getArticleClickAndLike()
        .then((res) => {
          this.allData = res.data.list;
          // console.log(this.allData)
          //从大到小进行
          this.allData.likes.sort((a, b) => {
            return b - a;
          });
          // console.log(this.allData);
          this.updateChart();
          this.startInterval();
        })
        .catch((error) => {
          console.log("error");
        });
    },
    updateChart() {
      const colorArr = [
        ["#b6a2de", "#5ab1ef"],
        ["#b6a2de", "#5ab1ef"],
        ["#b6a2de", "#5ab1ef"],
      ];

      const titleArr = this.allData.title.map((item) => {
        return item;
      });
      const numArr = this.allData.likes.map((item) => {
        return item;
      });

      const dataOption = {
        xAxis: {
          data: titleArr,
        },
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: numArr,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 10) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 5) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize =
        (this.$refs.article_learn_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.likes.length - 1) {
          this.startValue = 0;
          this.endValue = 5;
        }
        this.updateChart();
      }, 2000);
    },
  },
};
</script>

<style lang='less' scoped>
</style>
